<script>
import defaultImg from '@/assets/default.png'
import { Setting, SwitchButton } from '@element-plus/icons-vue'

export default {
  name: "Main_Header",
  components: {
    Setting,
    SwitchButton
  },
  data() {
    return {
      defaultImg, // 默认头像
      username: '',
      imageUrl: '',
    }
  },
  methods: {
    logout() {
      localStorage.clear() // 清空本地存储
      this.$router.push('/login')
    }
  },
  mounted() {
    // 获取用户名和头像
    this.username = localStorage.getItem('username')
    this.imageUrl = localStorage.getItem('imageUrl')
  }
}
</script>

<template>
  <div>
    <el-row>
      <el-col class="nav_header" :span="2" :offset="22">
        <el-dropdown trigger="click">
          <div style="display: flex; align-items: center; cursor: pointer;">
            <el-avatar
                :size="24"
                fit="cover"
                :src="imageUrl || defaultImg"
                style="margin-right: 8px;"
            />
            <span>{{ username }}</span>
          </div>
          <template #dropdown>
            <el-dropdown-menu>
              <el-dropdown-item>
                <el-icon><Setting /></el-icon>
                <span style="margin-left: 8px;">个人设置</span>
              </el-dropdown-item>
              <el-dropdown-item @click="logout">
                <el-icon><SwitchButton /></el-icon>
                <span style="margin-left: 8px;">退出登录</span>
              </el-dropdown-item>
            </el-dropdown-menu>
          </template>
        </el-dropdown>
      </el-col>
    </el-row>
  </div>
</template>

<style scoped lang="less">
.nav_header {
  margin-top: 10px;

  :deep(.el-dropdown) {
    cursor: pointer;
  }

  span {
    font-size: 14px;
  }
}
</style>